package com.red.springmanual.usespring;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class UserService implements UserServiceInterface{

    private final OrderService orderService;

    private User admin;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * byType byName
     * @param orderService
     */
    public UserService(OrderService orderService) {
        this.orderService = orderService;
        System.out.println(1);
    }

    @PostConstruct
    public void init(){
        admin = new User();
        admin.setName("张三");
        admin.setPwd("xxx");
    }

    public User getAdmin() {
        return admin;
    }


    /**
     * 开启事务
     * 1.事物管理器新建一个数据库链接
     * 2.conn.autocommit =false;
     */
    @Override
    @Transactional
    public void test(){
        System.out.println(orderService);
        jdbcTemplate.execute("delete from TB_USER");

        jdbcTemplate.execute("INSERT INTO TB_USER (USER_ID,USER_NAME) VALUES(1,'赵二')");
//        int i =1/0;
        jdbcTemplate.execute("INSERT INTO TB_USER (USER_ID,USER_NAME) VALUES(2,'高玉')");
   /*     Map<String, Object> result = jdbcTemplate.queryForMap("select * from tb_user");
        result.forEach((k,v)-> System.out.println("查询结果为->"+k+":"+v));*/
    }
}
